package cn.tedu.egmvc03.controller;

import cn.tedu.egmvc03.mapper.UserMapper;
import cn.tedu.egmvc03.pojo.dto.UserDto;
import cn.tedu.egmvc03.pojo.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("users/")
public class UserController {
    @Autowired(required = false)
    private UserMapper userMapper;


    @RequestMapping("insert")
    public String insertUser(UserDto userDto){
        UserVo userVo=userMapper.selectOneUser(userDto.getUsername());
        if (userVo!=null){
            return "用户名已存在！";
        }
        int i = userMapper.insertUser(userDto);
        if (i==1)
        return "注册成功！";
        return "服务器繁忙!";
    }


    @RequestMapping("delete")
    public String  deleteUser(String username){
        UserVo userVo=userMapper.selectOneUser(username);
        if (userVo!=null){
            int i = userMapper.deleteUser(username);
            if (i==1)
                return "注销成功！";
            return "服务器繁忙!";

        }
        return "该用户不存在！";
    }



    @RequestMapping("update")
    public String updateUser(UserVo userVo){
        UserVo userVodb=userMapper.selectOneUser(userVo.getUsername());
        if (userVo!=null){
            int i = userMapper.updateUser(userVo);
            if (i==1)
                return "更新成功！";
            return "服务器繁忙";
        }
        return "该用户不存在";
    }



    @RequestMapping("selectAll")
    public List<UserVo> selectAllUSer(){
        return userMapper.selectAllUser();
    }


    @RequestMapping("selectone")
    public UserVo selectUSer(String username){
        UserVo userVo=userMapper.selectOneUser(username);
        if (userVo!=null){
            return userVo;
        }
        return null;
    }
}
